package com.weixin.paleituo.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.LocationArrowRecyclerViewAdapter;
import com.weixin.paleituo.eventbus.LocationCityEvent;
import com.weixin.paleituo.util.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Author yichao
 * Time  2017/12/10 20:06
 * Dest  定位城市
 */

public class LocationCityPopu extends PopupWindow {

    private View rootView;
    private Context context;

    public void setDialogType(String dialogType) {
        mDialogType = dialogType;
    }
    private String mDialogType;
    private String mChekStr;
    private RecyclerView mRecyclerView;
    private LocationArrowRecyclerViewAdapter mLocationArrowRecyclerViewAdapter;


    private List<String> mStringList=new ArrayList<>();

    public LocationCityPopu(Context context) {
        super(context);
        this.context = context;
        init();
    }

    private void init() {
        rootView = LayoutInflater.from(context).inflate(R.layout.coupons_location_popuwindow_layout, null);
        // TODO: 2016/5/17 设置动画
       // setAnimationStyle(R.style.dialog);

        // TODO: 2016/5/17 设置背景颜色
      setBackgroundDrawable(new ColorDrawable(0x90000000));
        // TODO: 2016/5/17 设置可以获取焦点
      setFocusable(true);
        // TODO: 2016/5/17 设置可以触摸弹出框以外的区域
      setOutsideTouchable(true);
        // TODO：更新popupwindow的状态
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);

        setContentView(rootView);

        mRecyclerView = rootView.findViewById(R.id.recycle_location_city_popu);
      RelativeLayout relativeLayout= rootView.findViewById(R.id.ll_contentroot_arrow_popuwindow_layout_coupons);
        relativeLayout.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              dismiss();
          }
      });
        mLocationArrowRecyclerViewAdapter = new LocationArrowRecyclerViewAdapter(context, mStringList);

        //设置布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
        //设置adapter
        mRecyclerView.setAdapter(mLocationArrowRecyclerViewAdapter);
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //添加分割线
    /*    mRecyclerView.addItemDecoration(new DividerItemDecoration(
                context, DividerItemDecoration.VERTICAL));
*/

        mLocationArrowRecyclerViewAdapter.setOnItemClickListener(new LocationArrowRecyclerViewAdapter. OnItemClickListener(){
            @Override
            public void onClick(View v, int position) {
                String s = mStringList.get(position);
                EventBus.getDefault().post(new LocationCityEvent(s));

                mLocationArrowRecyclerViewAdapter.setCheckData(s);
                mLocationArrowRecyclerViewAdapter.notifyDataSetChanged();
                dismiss();

            }
        });

    }


    public View getView() {
        return rootView;
    }

    /**
     * 设置选中的
     * @param checkStr
     */
    public void setChekStr(String checkStr) {

        mChekStr = checkStr;
        mLocationArrowRecyclerViewAdapter.setCheckData(mChekStr);
    }

    /**
     * 设置数据
     * @param stringList
     */

    public void setList(List<String> stringList) {
        mStringList.clear();
        mStringList.addAll(stringList);

        if (mStringList.size()>6){
            ViewGroup.LayoutParams layoutParams = mRecyclerView.getLayoutParams();
            layoutParams.height= UIUtils.getScreenHeigh(context)/2;
            mRecyclerView.setLayoutParams(layoutParams);
        }
        mLocationArrowRecyclerViewAdapter.notifyDataSetChanged();

    }

    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24){
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            setHeight(height);
        }


        super.showAsDropDown(anchor, xoff, yoff);
    }
}
